package com.zhuliwei.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.CategoryDto;
import com.zhuliwei.domain.entity.Category;
import com.zhuliwei.domain.vo.CategoryVo;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.service.CategoryService;
import com.zhuliwei.utils.BeanCopyUtils;
import com.zhuliwei.utils.WebUtils;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理接口控制层
 *
 * @author 朱力维
 * @time 2022/10/28 9:43
 **/
@RestController
@RequestMapping("/content/category")
@Api(tags="分类",description = "分类管理相关接口")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<CategoryVo> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/list")
    @PreAuthorize("@ps.hasPermission('content:category:list')")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, CategoryDto categoryDto){
        return categoryService.pageCategoryList(pageNum,pageSize,categoryDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryDetail(@PathVariable String id){
        Category category = categoryService.getById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @PutMapping
    @PreAuthorize("@ps.hasPermission('content:category:update')")
    public ResponseResult updateCategory(@RequestBody CategoryDto categoryDto){
        if (!StringUtils.hasText(categoryDto.getName())){
            return ResponseResult.errorResult(500,"分类名称不能为空");
        }
        if (!StringUtils.hasText(categoryDto.getStatus())){
            return ResponseResult.errorResult(500,"请选择状态");
        }
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @PostMapping
    @PreAuthorize("@ps.hasPermission('content:category:add')")
    public ResponseResult addCategory(@RequestBody CategoryDto categoryDto){
        if (!StringUtils.hasText(categoryDto.getName())){
            return ResponseResult.errorResult(500,"分类名称不能为空");
        }
        if (!StringUtils.hasText(categoryDto.getStatus())){
            return ResponseResult.errorResult(500,"请选择状态");
        }
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        categoryService.save(category);
        return ResponseResult.okResult();
    }
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ps.hasPermission('content:category:delete')")
    public ResponseResult delCategory(@PathVariable List<String> ids){;
        return categoryService.delCategoryByIds(ids);
    }
    @GetMapping("/categoryData")
    public ResponseResult categoryData(){
        return categoryService.getCategoryData();
    }

    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categorys = categoryService.list();
            List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categorys, CategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), CategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(categoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
}
